package com.zdft.promotion.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Administrator
 * @date 2019/5/8
 **/
public class DateUtils {

    public static Date offset(Date date, int number, int offset) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(number, offset);
        return cal.getTime();
    }

    public static String format(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static Date parse(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return null;
    }

    public static Date parse(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(strDate);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return null;
    }

    private static Date parseError(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
        try {
            return format.parse(strDate);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return null;
    }

    public static Date parseErrorDate(String strDate) {
        Date result = parse(strDate);
        if (result == null) {
            return parseError(strDate);
        }
        return result;
    }
}
